<template>
  <a-drawer
    width="576"
    :title="formTitle"
    :label-col="4"
    :wrapper-col="14"
    :visible="open"
    :body-style="{height:'calc(100vh - 100px)',overflow:'auto'}"
    @close="cancel">
    <a-form-model ref="form" :model="form" :rules="rules" layout="vertical">
      <a-spin :spinning="spinning" :delay="delayTime" tip="Loading...">
        <a-row :gutter="32">
          <a-col :span="12">
            <a-form-model-item label="新文件名" prop="fileName">
              <a-input v-model="form.newFileName" placeholder="请输入新文件名"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-spin>
      <div class="bottom-control">
        <a-space>
          <a-button :disabled="disabled" @click="cancel">
            取消
          </a-button>
          <a-button type="primary" :disabled="disabled" @click="submitForm">
            保存
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>
<script>

import AntModal from '@/components/pt/dialog/AntModal'
import { renameFile } from '@/api/dockerinsight/fileSystem'

export default {
  name: 'FileSystemEditForm',
  components: {
    AntModal
  },
  props: {
    currentPath: {
      type: String,
      required: true
    },
    ip: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '重命名',
      // 表单参数
      form: {},
      fileName: undefined,
      rules: {
      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        newFileName: undefined
      }
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.form.newFileName = row.fileName
      this.fileName = row.fileName
    },
    /** 提交按钮 */
    submitForm () {
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          this.spinning = !this.spinning
          const params = {}
          params.ip = this.ip
          params.currentPath = this.currentPath
          params.fileName = this.fileName
          params.newFileName = this.form.newFileName
          renameFile(params).then(response => {
            this.$message.success('更新成功', 3)
            this.open = false
            this.$emit('ok')
            this.$emit('close')
            this.disabled = false
            this.spinning = !this.spinning
          }).catch(error => {
            this.$message.error(error)
            this.disabled = false
            this.spinning = !this.spinning
          })
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/dockerinsight/filesystem/index'
      this.$router.push(index)
    }
  }
}

</script>
